package com.zhkj.txg.module.member.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.member.vm.MemberListViewModel
import com.zhkj.txg.module.order.adapter.OrderDetailInfoAdapter
import com.zhkj.txg.module.order.ui.OrderShippingActivity
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_member_detail.*
import kotlinx.android.synthetic.main.item_order_detail_address.*
import kotlinx.android.synthetic.main.item_order_detail_data.*
import kotlinx.android.synthetic.main.item_order_detail_shipping.*
import kotlinx.android.synthetic.main.item_order_detail_status.*
import kotlinx.android.synthetic.main.item_order_goods.*

class MemberOrderDetailActivity : ViewModelActivity<MemberListViewModel>() {

    companion object {
        private val ORDER_ID = "orderId"
        fun open(activity: Activity, orderId: Long) {
            val intent = Intent(activity, MemberOrderDetailActivity::class.java)
                .putExtra(ORDER_ID, orderId)
            activity.startActivity(intent)
        }
    }

    private val infoAdapter = OrderDetailInfoAdapter()

    override fun statusTitleBar(): View? = titleMemberDetail
    override fun setViewModel(): MemberListViewModel = createViewModel()
    override val layoutResId: Int = R.layout.activity_member_detail

    override fun initUI() {
        with(rvOrderDetailInfo) {
            layoutManager = LinearLayoutManager(this@MemberOrderDetailActivity)
            adapter = infoAdapter
        }
        btnShipping.setOnClickListener {
            OrderShippingActivity.start(this, intent.getLongExtra(ORDER_ID, 0))
        }
    }

    override fun initData() {
        viewModel.memberOrderDetail(intent.getLongExtra(ORDER_ID, 0))
    }

    override fun initViewModel() {
        viewModel.memberOrderDetailLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                // 设置顶部状态和底部按钮的显示
                if (it.shippingStatus != null && it.shippingStatus == "1") {
                    val drawable = getDrawable(R.drawable.ic_order_status_wait_pay)
                    drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                    tvOrderDetailStatus.setCompoundDrawablesRelative(null, null, drawable, null)
                    tvOrderDetailStatus.text = getString(R.string.member_send)
                    btnShipping.visibility = View.VISIBLE
                } else {
                    tvOrderDetailStatus.text = getString(R.string.non_delivery)
                    btnShipping.visibility = View.GONE
                }
                tvOrderDetailShipping.text = "暂无物流信息"
                tvOrderDetailShippingTime.visibility = View.GONE
                // 设置收货地址
                tvOrderDetailConsignee.text = it.consignee
                tvOrderDetailMobile.text = it.mobile
                tvOrderDetailAddress.text = it.fullAddress
                // 设置商品详情
                tvOrderGoodsName.text = it.goodsName
                tvOrderGoodsPrice.text = SpanUtil.setMoneySymbol(it.goodsPrice ?: "0.0")
                tvOrderGoodsNumber.text = "x${it.goodsNum}"
                ImageUtils.loadRoundCornerImage(this, ivOrderGoods, it.image ?: "")
//                // 判断是否显示赚
//                if (it.shareEarn != null && it.shareEarn > 0)
//                tvOrderGoodsEarn.text = SpanUtil.setPrefixMoneyStr(price = it.shareEarn.toString())
                // 设置订单信息
                infoAdapter.replaceData(it.orderInfo)
                // 设置底部数量价格
                tvTotalCount.text = "共${it.goodsNum}件，合计"
                tvTotalAmount.text = SpanUtil.setMoneySymbol(price = it.orderAmount ?: "0.0")
            }
        })
        viewModel.shippingLiveData.observe(this, Observer {
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                if (it.response?.data != null && it.response?.data?.isNotEmpty() == true) {
                    tvOrderDetailShipping.text = it.response?.data?.get(0)?.context
                    tvOrderDetailShippingTime.visibility = View.VISIBLE
                    tvOrderDetailShippingTime.text = it.response?.data?.get(0)?.finalTime
                }
            }
        })
    }
}